<?php
class Controller_Machine extends Controller 
{
	public function action_report ()
	{
		$post = $_POST;
		Kohana::$log->add(Log::DEBUG, print_r($post, TRUE));

		if (! Data::valid($post))
			throw new HTTP_Exception_404("NOT FOUND");

		Data::record_report($post);
	}

    public function action_download($type)
    {
        $id = $this->request->query('id');
        if (empty($id))
            return;

        if ($type == 'apklist')
        {
            $content = download::contents('apk.list', $id);
            header("Content-type: application/octet-stream\n");
            echo $content;exit;
        }
        else if ($type == 'md5txt')
        {
            $content = download::contents('md5.txt', $id);
            header("Content-type: text/plain\n");
            echo $content;exit;
        }
        else if ($type == 'apk')
        {
            $apk = $this->request->query('apk');
            if (empty($apk))
                return;

            $this->request->redirect('/bjm/'.$apk);
        }
    }

    public function action_rom($type = 'report')
    {
        $post = $this->request->post();
        $keys = array('brand', 'imei', 'type', 'apps', 'version', 'auth', 'no');

        $data = array();
        foreach ($post as $key => $val)
        {
            if (in_array($key, $keys))
                $data[$key] = trim($val);
        }

        Kohana::$log->add(Log::DEBUG, print_r($data, TRUE));
        echo "OK";exit;
    }
}
